package com.starlink.model.dtomapper;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.starlink.model.domain.SchoolbagDO;
import com.starlink.model.request.SchoolbagRequest;
import com.starlink.model.vo.SchoolbagVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

import java.util.Optional;

/**
 * @author zhanghong
 */
@Mapper(componentModel = "spring", imports = {JSON.class, Optional.class, Lists.class})
public interface SchoolbagDtoMapper {

    SchoolbagDtoMapper INSTANCE = Mappers.getMapper(SchoolbagDtoMapper.class);

    /**
     * DO to VO
     *
     * @param schoolbagDO
     * @return
     */
    @Mappings({
            @Mapping(target = "images", expression = "java(Optional.ofNullable(schoolbagDO.getImages()).map(item -> JSON.parseArray(item, String.class)).orElse(Lists.newArrayList()))"),
            @Mapping(target = "themeLabel", expression = "java(Optional.ofNullable(schoolbagDO.getThemeLabel()).map(item -> JSON.parseArray(item, String.class)).orElse(Lists.newArrayList()))"),
            @Mapping(target = "readGuide", expression = "java(Optional.ofNullable(schoolbagDO.getReadGuide()).map(item -> JSON.parseArray(item, String.class)).orElse(Lists.newArrayList()))"),
            @Mapping(target = "normalLabel", expression = "java(Optional.ofNullable(schoolbagDO.getNormalLabel()).map(item -> JSON.parseArray(item, String.class)).orElse(Lists.newArrayList()))")
    })
    SchoolbagVO do2VO(SchoolbagDO schoolbagDO);

    /**
     * request to DO
     *
     * @param request
     * @return
     */
    @Mappings({
            @Mapping(target = "images", expression = "java(Optional.ofNullable(request.getImages()).map(JSON::toJSONString).orElse(null))"),
            @Mapping(target = "themeLabel", expression = "java(Optional.ofNullable(request.getThemeLabel()).map(JSON::toJSONString).orElse(null))"),
            @Mapping(target = "readGuide", expression = "java(Optional.ofNullable(request.getReadGuide()).map(JSON::toJSONString).orElse(null))"),
            @Mapping(target = "normalLabel", expression = "java(Optional.ofNullable(request.getNormalLabel()).map(JSON::toJSONString).orElse(null))")
    })
    SchoolbagDO request2DO(SchoolbagRequest request);
}
